package paa.simulator;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import paa.crawlers.delicious.Crawler;
import paa.simulator.grafo.GraphBuilder;
import paa.simulator.grafo.GraphFromData;
import paa.simulator.grafo.PessoaVertex;
import paa.simulator.grafo.GraphBuilder.Type;
import paa.simulator.grafo.simple.UndirectedSimpleGraph;

public class GraphSaver {

	public static void main(String[] args) throws Exception {
		if (args == null || args.length < 4) {
			System.err.println("usage: java " + Crawler.class + " <in dir> <graph type> <common threshold> <out file>");
			System.exit(1);
		}
		
		File dir = new File(args[0]);
		if (!dir.exists()) {
			System.err.println("Directory does exists, exiting.");
			System.exit(1);
		}
		
		//Lendo arquivos
		File[] subdirs = dir.listFiles();
		if (subdirs == null || subdirs.length == 0) {
			System.err.println("Directory is empty, exiting.");
			System.exit(1);
		}

		//Creating graph
		Type type = GraphBuilder.Type.valueOf(args[1]);
		double commonThreshold = Double.parseDouble(args[2]);
		
		File out = new File(args[3]);
		if (out.exists()) {
			System.err.println("Output already exists, exiting.");
			System.exit(1);
		}
		
		UndirectedSimpleGraph<PessoaVertex> graph = GraphFromData.createGraph(subdirs, commonThreshold, type);
		
		ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(out)));
		oos.writeObject(graph);
		oos.flush();
		oos.close();
	}	
}